from json import dumps

from flask import make_response, current_app


def output_json(data, code, headers=None):

    if 'message' not in data:
        data = {
            'message': 'OK',
            'data':data
        }

    settings = current_app.config.get('RESTFUL_JSON', {})
    dumped = dumps(data, **settings) + "\n"
    resp = make_response(dumped, code)
    resp.headers.extend(headers or {})
    return resp

